package com.easywan.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringsUtils {

	public static String findMid(String html, String string, String string2) {
		int start = html.indexOf(string) + string.length();
		int end = html.lastIndexOf(string2);
		if(start > end) return "";
		return html.substring(start,end);
	}

	public static List<String> findMidAll(String html, String string1, String string2) {
		String regEx = string1 + ".*" + string2;
		//System.out.println(regEx);
		Pattern pattern = Pattern.compile(regEx);
		Matcher matches= pattern.matcher(html);
        List<String> result = new ArrayList<>();
        while (matches.find()) {
          String found= matches.group();
          result.add(found);
        }
		return result;
		
	}

	public static String firestToUpper(String action) {
		if(action == null){
			return null;
		}
		String first = action.substring(0, 1).toUpperCase();
		return first+action.substring(1);
	}

	public static List<String> mathes(String html, String regex) {
		return mathesAndNot(html,regex,null);
	}

	public static List<String> mathes2(String html, String hrefRegex) {
		List<String> list = new ArrayList<>();
		String[] ss = hrefRegex.split("000");
		
		String start = ss[0];
		String end = ss[1];
		for(int i = 0; i < html.length() ; i++){//i:已经匹配到的位置
			char c = html.charAt(i);
			if( c == start.charAt(0)){
				int j = 1 ;//j:开始匹配字符的长度
				for(; j < start.length();j++){
					char c2 = html.charAt(j+i);
					if( c2 == start.charAt(j)){
						
					}else{
						break;
					}
				}
				if(j == start.length()){//头匹配成功
					for(int k = 0; k < html.length()- i - j; k++){//所匹配字符的长度
						if(html.charAt(i+j+k) == end.charAt(0)){
							int l = 1;//结尾字符的长度
							for(;l < end.length();l++){
								if(html.charAt(i+j+k+l) == end.charAt(l)){
									l++;
								}else{
									break;
								}
							}
							if(l == end.length()){//匹配成功
								list.add(html.substring(i+j,i+j+k));
								i = i+j+k+l - 1;//跳到匹配结束处继续处理
								break;
							}else{
								
							}
						}
					}
				}
			}
		}
		
		return list;
	}

	public static List<String> mathesAndNot(String htmlContent, String regex,String not) {
		List<String> list = new ArrayList<>();
		Pattern pattern =Pattern.compile(regex);
		Matcher m = pattern.matcher(htmlContent);
		while(m.find()){
			String s = m.group(1);
			if(stringsIsNotNull(not)){
				if(s.indexOf(not) < 0)
					list.add(s);
			}else{
				list.add(s);
			}
		}
		return list;
	}

	public static boolean stringsIsNotNull(String not) {
		if(not == null || not.trim().length() == 0)
			return false;
		else{
			return true;
		}
	}

	public static List<String> urlToStrings(String pageUrl, int start, int end, int loop, String gz) {
		List<String>  rs = new ArrayList<>();
		for(int i = start; i < end; i += loop){
			rs.add(pageUrl.replace(gz, i+""));
		}
		return rs;
	}
	public  static String listToString(List<String> list, char separator) {    
		StringBuilder sb = new StringBuilder();     
		for (int i = 0; i < list.size(); i++) {        
			sb.append(list.get(i));        
			if (i < list.size() - 1) {            
				sb.append(separator);        
			}    
		}    
		return sb.toString();
	}
	
	public static String html2Text(String inputString) { 
        String htmlStr = inputString; // 含html标签的字符串 
        String textStr = ""; 
        java.util.regex.Pattern p_script; 
        java.util.regex.Matcher m_script; 
        java.util.regex.Pattern p_style; 
        java.util.regex.Matcher m_style; 
        java.util.regex.Pattern p_html; 
        java.util.regex.Matcher m_html; 
        try { 
         String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script>]*?>[\s\S]*?<\/script> 
         // } 
         String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style> 
         // } 
         String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 

         p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); 
         m_script = p_script.matcher(htmlStr); 
         htmlStr = m_script.replaceAll(""); // 过滤script标签 

         p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); 
         m_style = p_style.matcher(htmlStr); 
         htmlStr = m_style.replaceAll(""); // 过滤style标签 

         p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); 
         m_html = p_html.matcher(htmlStr); 
         htmlStr = m_html.replaceAll(""); // 过滤html标签 

         textStr = htmlStr; 

        } catch (Exception e) { 
         System.err.println("Html2Text: " + e.getMessage()); 
        } 

        return textStr; 
      }

	public static List<String> mathesAnd(String htmlContent, String regex, String not) {
		List<String> list = new ArrayList<>();
		Pattern pattern =Pattern.compile(regex);
		Matcher m = pattern.matcher(htmlContent);
		while(m.find()){
			String s = m.group(1);
			//System.out.println(s);
			if(stringsIsNotNull(not)){
				if(s.indexOf(not) >= 0)
					list.add(s);
			}else{
				list.add(s);
			}
		}
		return list;
	}

	public static String del(String url,String string) {
		
		return url.replace(string, "");
	}

	public static Boolean isBank(String name) {
		if(null == name) return true;
		if(name.trim().equals("")) return true;
		return false;
	}

	public static Boolean isNotBank(String name) {
		// TODO Auto-generated method stub
		return !isBank(name);
	} 
}
